define('apps/common/RelationOpts', ['core/jQuery', 'apps/Dialog/Confirm', 'apps/Dialog/Tip', 'core/Toggle', 'core/ajax/Ajax'], function(require) {
    
    var $ = require('core/jQuery'),
        Confirm = require('apps/Dialog/Confirm'),
        Tip = require('apps/Dialog/Tip'),
        Toggle = require('core/Toggle'),
        Ajax = require('core/ajax/Ajax'),
        
        FollowClass = {
            's_0' : 'b_concern',
            's_1' : 'b_concerned',
            's_2' : 'b_concern',
            's_3' : 'b_eachOther',
            
            'b_0' : 'b_concern_b',
            'b_1' : 'b_concerned_b',
            'b_2' : 'b_concern_b',
            'b_3' : 'b_eachOther_b'
        }
        
        URL = {
            'friendnew' : '/friend/ajax/friend_new.php',
            'frienddel' : '/friend/ajax/friend_del.php'
        },
        
        TPL = {
            'followdrop' : '<div style="left:0;width:120px;" class="ui_list abs _j_followdropcnt dni">\
                                <ul class="ui_list_bd"><li><a href="#" class="_j_unfollow">取消关注</a></li></ul>\
                            </div>'
        };
        
    function RelationOpts(opts) {
        this.context = 'body';
        this.triggerSelector = '._j_g_follow';
        this.dropTPL = TPL.followdrop;
        K.mix(this, opts);

        this.init();
    }
    
    RelationOpts.prototype = {
        
        'init' : function() {
            this.bindEvents();
        },
        
        'bindEvents' : function() {
            $(this.context)
                .delegate('.'+FollowClass['s_1'] + ',.' + FollowClass['s_3'] + ',.' + FollowClass['b_1'] + ',.' + FollowClass['b_3'], 'click', $.proxy(this.showFollowDrop, this)) // 下拉
                .delegate('.'+FollowClass['s_0'] + ',.' + FollowClass['b_0'], 'click', $.proxy(this.followFriend, this)) // 关注好友
                .delegate('._j_unfollow', 'click', $.proxy(this.unfollowFriend, this)); // 取消关注
        },
        
        /**
         * 创建显示下拉框
         */
        'showFollowDrop' : function(ev) {
            
            var _this = this,
                target = $(ev.currentTarget);
            
            if(this.target) {
                this.target.children('a').removeClass('clicked');
            }
            
            // 为好友关注状态构建分组下拉框
            if(!this.followDropCnt) {
            	this.createFollowDropCnt();
            }
            
            if(!this.target || this.target.get(0) !== target.get(0)) {
                
                this.target = target;
                
                // 为好友关注状态构建分组下拉框
                if(!this.followDropCnt) {
                	this.createFollowDropCnt();
                }

                this.target.children('a').addClass('clicked');
                this.setDropPosition();
                this.followDropCnt.removeClass('dni');
                
            } else {
            	
            	// 为好友关注状态构建分组下拉框
                if(!this.followDropCnt) {
                	this.createFollowDropCnt();
                }
                               
                // 定位，显隐
                if(this.followDropCnt.hasClass('dni')) {
                    this.target.children('a').addClass('clicked');
                    this.setDropPosition();
                }
                this.followDropCnt.toggleClass('dni');
            }

            ev.preventDefault();
        },
        
        'createFollowDropCnt' : function() {
        	this.followDropCnt = $(this.dropTPL).appendTo(this.context);
            new Toggle({
                'trigger' : this.triggerSelector,
                'board' : this.followDropCnt,
                'handler' : $.proxy(function() {
                    this.followDropCnt.addClass('dni');
                    this.target.children('a').removeClass('clicked');
                }, this)
            });
        },
        
        /**
         * 下拉框定位
         */
        'setDropPosition' : function() {
            var targetOffset = this.target.offset();
            this.followDropCnt.css({
                'top' : targetOffset.top + this.target.outerHeight(),
                'left' : targetOffset.left
            });
        },

        /**
         * 关注好友
         */
        'followFriend' : function(ev) {
        	ev.preventDefault();
        	
        	var target = $(ev.currentTarget);
            this.target = target;
            
            // 防止反复点击
            if(target.data('changing')) return;
            target.data('changing', true);
            
            var _this = this,
                fuid = target.data('fuid');
            Ajax.post(URL.friendnew, this.target.data(), 
	            function(data) {
                    _this.updateFUserStatus(target, data.isfriend);
                    K.fire('friend:follow', fuid, data.isfriend);
	            },
	            function(error) {
	            	target.data('changing', false);
                    K.log('Data Err:', error);
	            }
            );
        },
        
        /**
         * 取消关注
         */
        'unfollowFriend' : function(ev) {
        	ev.preventDefault();
        	
            var _this = this,
                fuid = this.target.data('fuid'),
                target = this.target;
                
            // 防止反复点击
            if(target.data('changing')) return;
            target.data('changing', true);
            
            Confirm({
                'width' : 360,
                'title' : '取消关注',
                'content' : '<div class="pop_alert tac rel" style="height:71px;"><span class="infor">确定不再关注<a href="/user/?uid=' + this.target.data('fuid') + '">' + this.target.data('funame') + '</a>吗？</span></div>'
            }, function() {
                Ajax.post(URL.frienddel, _this.target.data(), 
	                function(data) {
                        Tip({
                            'width' : 360,
                            'style' : 0,
                            'PANEL_CLASS' : 'dialog dialog_small',
                            'content' : '<div style="height:95px;padding-top:72px;" class="pop_alert tac rel"><span class="infor_alert"><i class="spr_icons i_ok_m"></i>&nbsp;取消关注成功</span></div>',
                            'autoHide' : true
                        });
                        _this.updateFUserStatus(target, data.isfriend);
                        K.fire('friend:unfollow', fuid, data.isfriend);
	                },
	                function(error) {
	                	target.data('changing', false);
                        K.log('Err Data:', error);
	                }
                );
            });
        },
        
        'updateFUserStatus' : function(target, isfriend) {
            var prevClass = target.data('followclass'),
                type = /_b$/.test(prevClass) ? 'b' : 's',
                currClass = FollowClass[type+'_'+isfriend],
                relOpts = $(this.triggerSelector+'[data-fuid="'+target.data('fuid')+'"]');

            $('[data-fuid="'+target.data('fuid')+'"]').data('isfriend', isfriend);
            relOpts.fadeOut(function() {
            	$(this).removeClass(prevClass).addClass(currClass).data('followclass', currClass).fadeIn(function() {
            		target.data('changing', false);
            	});
            });
        }
    };
    
    return RelationOpts;
});